import org.apache.http.HttpHost;
import pipeline.KuaiIpPipeline;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * Created by zhangcheng on 17/4/18.
 */
public class KuaiIpPageProcessor implements PageProcessor {

    static final String URL_POST = "http://www.kuaidaili.com/free/inha/\\d+/";

    private Site site = Site.me()
            .setRetryTimes(3)
            .setHttpProxy(new HttpHost("10.101.1.6",80))
            .setSleepTime(1000)
            .setTimeOut(10000);
    public void process(Page page) {
        page.addTargetRequests(page.getHtml().links().regex(URL_POST).all());
        page.putField("ip", page.getHtml().xpath("//*[@id=\"list\"]/table/tbody/tr/td[1]/text()").all());
        page.putField("port", page.getHtml().xpath("//*[@id=\"list\"]/table/tbody/tr/td[2]/text()").all());
    }

    public KuaiIpPageProcessor() throws Exception{
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) throws Exception{
        Spider.create(new KuaiIpPageProcessor())
                .addUrl("http://www.kuaidaili.com/free/inha/1/")
                .addPipeline(new KuaiIpPipeline())
                .run();
    }
}
